package com.learn.ch02ttlqueue.confim_advance.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/08/24
 * @description ch08 发布确认高级
 * 备份交换机
 */
@Configuration
public class RabbitConfig {
    public static final String CONFIRM_EXCHANGE = "confirm_exchange";
    public static final String CONFIRM_QUEUE = "confirm_queue";
    public static final String CONFIRM_ROUTING_KEY = "confirm_routing_key";

    public static final String BACK_EXCHANGE = "backup_exchange";
    public static final String BACK_QUEUE = "backup_queue";
    public static final String WARNING_QUEUE = "warning_queue";

    //声明业务 Exchange
    @Bean
    public DirectExchange confirmExchange() {
        // 声明备份交换机时，需要删除原先的交换机
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE)
                .durable(true)
                .withArgument("alternate-exchange",BACK_EXCHANGE)
                .build();
    }
    @Bean
    public FanoutExchange backupExchange() {
        return new FanoutExchange(BACK_EXCHANGE);
    }

    // 声明确认队列
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }
    @Bean
    public Queue backQueue() {
        return QueueBuilder.durable(BACK_QUEUE).build();
    }
    @Bean
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    // 声明确认队列绑定关系
    @Bean
    public Binding queueBinding(@Qualifier("confirmQueue") Queue queue,
                                @Qualifier("confirmExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY);
    }

    @Bean
    public Binding backupQueueBinding(@Qualifier("backQueue") Queue backQueue,
                                @Qualifier("backupExchange") FanoutExchange exchange) {
        // 没有with
        return BindingBuilder.bind(backQueue).to(exchange);
    }
    @Bean
    public Binding warningQueueBinding(@Qualifier("warningQueue") Queue warningQueue,
                                @Qualifier("backupExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(warningQueue).to(exchange);
    }
}
